//
//  WeatherForecastViewController.m
//  ZCMobileApp
//
//  Created by lizhiwei on 12-12-18.
//  Copyright (c) 2012年 lizhiwei. All rights reserved.
//

#import "WeatherForecastViewController.h"
#import "PageOneViewController.h"
#import "AppDelegate.h"
#import "Config.h"
#import "UIImageView+WebCache.h"
#import "RCAlertViewUtil.h"

@interface WeatherForecastViewController ()

@end

@implementation WeatherForecastViewController
@synthesize SetBtn;
@synthesize delegate;
@synthesize BackBtn;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadScrollViewWithPage:(UIView *)page
{
    int pageCount1 = [[myScrollView subviews] count];
    
    CGRect bounds = myScrollView.bounds;
    bounds.origin.x = bounds.size.width * pageCount1;
    bounds.origin.y = 0;
    page.frame = bounds;
    [myScrollView addSubview:page];
}

- (void)createPages
{
    CGRect pageRect = myScrollView.frame;
    
    //create pages
    
    for (int j =0;j<Count;j++)
    {
        NSString * CityName = [[delegate citysSelection] objectAtIndex:j];
        NSLog(@"----------------------%@",CityName);
        if (CityName == nil||[CityName isEqualToString:@""])
        {
            CityName = @"深圳";
            NSLog(@"==========SHEN");
        }
        else 
        {
            NSLog(@"==========HAHA");
        }
        
        NSBundle *bundle = [NSBundle mainBundle];
        NSURL *plistURL = [bundle URLForResource:@"CityCodeList" withExtension:@"plist"];
        NSDictionary *CityCodeListdictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];
        NSString * citynameoflist;
        NSString * citycodeoflist;
        for (int i=0; i<[CityCodeListdictionary count]; i++)
        {
            NSString *key = [[NSString alloc] initWithFormat:@"%i", i+1];
            NSDictionary *tmpDic = [CityCodeListdictionary objectForKey:key];
            citynameoflist = [tmpDic objectForKey:@"cityname"];
            citycodeoflist = [tmpDic objectForKey:@"citycode"];
            if ([CityName isEqualToString:citynameoflist])
            {
                NSString * citycode = citycodeoflist;
                PageOneViewController * pageview1 = [[PageOneViewController alloc]init];
                [AppDelegate app].CityCode = citycode;
                //[pageview1 viewDidLoad];
                UIView * page1 = pageview1.view;
                //page1.backgroundColor = [UIColor blueColor];
                [pageview1.view setFrame:pageRect];
                [self loadScrollViewWithPage:page1];
            }
        }
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //[self.navigationController removeFromParentViewController];
    float pageControlHeight = 18.0;
    NSString* pagecount = [delegate getPageCount];
    if ([pagecount isEqualToString:@""]||pagecount == nil)
    {
        int pageCount = 1;
        Count = pageCount;
    }
    else
    {
        NSLog(@"0000000000000000");
        int pageCount = pagecount.intValue;
        Count = pageCount;
    }
    
    CGRect scrollViewRect = [self.view bounds];
    scrollViewRect.size.height -= pageControlHeight;
    
    //create scrollview
    myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
    myScrollView.pagingEnabled = YES;
    myScrollView.contentSize = CGSizeMake(scrollViewRect.size.width * Count,1);
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.showsVerticalScrollIndicator = NO;
    myScrollView.delegate = self;
    
    //create pageview
    CGRect pageViewRect = [self.view bounds];
    pageViewRect.size.height = pageControlHeight;
    pageViewRect.origin.y = scrollViewRect.size.height;
    
    myPageControl = [[UIPageControl alloc] initWithFrame:pageViewRect];
    myPageControl.backgroundColor = [UIColor blackColor];
    myPageControl.numberOfPages = Count;
    myPageControl.currentPage = 0;
    [myPageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    
    //create pages
    [self createPages];
    
    //add to main view
    [self.view addSubview:myScrollView];
    [self.view addSubview:myPageControl];
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    NSString *bgimage= [accountDefaults objectForKey:@"UserDefaultBgKindKey"];
    
    if ([bgimage isEqualToString:@"蓝色主题风格"]) 
    {
        ADD_LEFT_BUTTONITEM_WEATHER(@"back.png");
    }
    else if([bgimage isEqualToString:@"棕色主题风格"])
    {
        ADD_LEFT_BUTTONITEM_WEATHER(@"back_01.png");
    }
    else if([bgimage isEqualToString:@"重大节日皮肤"])
    {
        NSData *dataImg_02=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_02];
        
        NSURL *urlSkinImg_02=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_02]];
       
        NSLog(@"-------找到333%@",urlSkinImg_02);
        
        //--------------------------
        UIButton * CallOutBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        CallOutBtn1.frame = CGRectMake(10, 8, 109/2, 57/2);
        UIImageView * Back = [[UIImageView alloc]init];
        [Back setImageWithURL:urlSkinImg_02 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(Back.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_02];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [Back setImage:[UIImage imageWithData:dataImg_02]];
        [CallOutBtn1 setImage:Back.image forState:UIControlStateNormal];
        [CallOutBtn1 addTarget:self action:@selector(ClickCallOutBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:CallOutBtn1];
        //--------------------------
        
    }

    //ADD_LEFT_BUTTONITEM(@"back.png");
    [self.view addSubview:SetBtn];
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = sender.frame.size.width;
    int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    myPageControl.currentPage = page;
}

- (void)changePage:(id)sender
{
    int page = myPageControl.currentPage;
    // update the scroll view to the appropriate page
    CGRect frame = myScrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [myScrollView scrollRectToVisible:frame animated:YES];
}

- (IBAction)SetCityButton:(id)sender
{    
    SetCity1ViewController *setcity = [[SetCity1ViewController alloc]initWithNibName:@"SetCity1ViewController" bundle:nil];
    //[self.navigationController pushViewController:setcity animated:YES];
    //setcity.delegate = self;
    
    setcity.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:setcity animated:YES];
    [setcity release];
}

-(void)initView
{
    if([self.view.subviews count] > 0) 
    {
		for (UIView *subview in self.view.subviews) 
        {
			[subview removeFromSuperview];
		}
	}
    [self viewDidLoad];
    /*UIButton * CallOutBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    CallOutBtn1.frame = CGRectMake(10, 10, 109/2, 57/2);
    [CallOutBtn1 setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [CallOutBtn1 addTarget:self action:@selector(ClickCallOutBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:CallOutBtn1];*/
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)Back:(id)sender
{
    NSLog(@"============您好");
    /*ViewController * home = [[ViewController alloc]init];
    home.modalTransitionStyle =UIModalTransitionStyleFlipHorizontal;
    [[AppDelegate app].unc presentModalViewController:home animated:YES];
    [home release];*/
    [self.navigationController popViewControllerAnimated:YES];
    //[self.navigationController dismissModalViewControllerAnimated:YES];
    
    //[[AppDelegate app].unc popToRootViewControllerAnimated:YES];
    /*for (UIViewController *controller in [[AppDelegate app].unc viewControllers])
    {
        if ([controller isKindOfClass:[ViewController class]])
        {
            [[AppDelegate app].unc popToViewController:controller animated:YES];
        }
    }*/
}

-(void) ClickCallOutBtn
{
     NSLog(@"-------hahaha");
     ViewController * home = [[ViewController alloc]init];
    [AppDelegate app].unc =nil;
    [AppDelegate app].unc = [[UINavigationController alloc] initWithRootViewController:home];
    [[AppDelegate app].window addSubview:[AppDelegate app].unc.view];
    [[AppDelegate app].window makeKeyAndVisible];
    [[AppDelegate app].unc popToRootViewControllerAnimated:YES];
}

@end
